﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cdn.Blogger.Models;
using System.Configuration;
using XZen.Blogger;

namespace Cdn.Blogger.Controllers
{
    public class HomeController : UIController
    {
        BlogService service;
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login(string ContentSetting = null)
        {
            CDNSetting setting = new CDNSetting();
            setting.ContentUri = "http://localhost";
            setting.Password = "$Rijusmit2014";
            setting.Subscription = "blogger";
            setting.User = "rijusmit";
            setting.UserName = "rijusmit";
           
            if (ContentSetting == "ContentSetting")
            {
                string connectoinstring = ConfigurationManager.AppSettings["ContentSetting"];
                //CDNSetting setting = new CDNSetting();
                setting.Parse(connectoinstring);
                Init(setting);
            }
            return View(setting);
        }

        [HttpPost]
        public ActionResult Login(CDNSetting setting)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Exception failx = Init(setting);
                    if (failx == null)
                    {
                        return RedirectToAction("Index", "BlogUI");
                    }
                    else
                    {
                        ModelState.AddModelError("Validation", "The settings you have added is not valid because " + failx.Message);
                        ViewBag.Error = "The settings you have added is not valid because " + failx.Message;
                    }
                }
                catch (Exception cools)
                {
                    ModelState.AddModelError("Validation", "The settings you have added is not valid because " + cools.Message);
                    ViewBag.Error = "The settings you have added is not valid because " + cools.Message;
                }
            }
            return View(setting);
        }
    }
}
